
<?php
/**
 * Created by PhpStorm.
 * User: xkq
 * Date: 2017/8/4 0004
 * Time: 20:49
 */
set_include_path(dirname(dirname(__FILE__)));
include_once("inc/init.php");
if (!session_id()) session_start();

$action = crequest("action");
$action = $action == '' ? 'list' : $action;

switch ($action)
{
	case "list":
		user_list();
		break;
	case "add_user":
		add_user();
		break;
	case "do_add_user":
		do_add_user();
		break;
	case "mod_dingwei":
		mod_dingwei();
		break;
	case "do_mod_user":
		do_mod_user();
		break;
	case "del_user":
		del_user();
		break;
	case "del_sel_user":
		del_sel_user();
		break;
}



/*------------------------------------------------------ */
//-- 案例列表
/*------------------------------------------------------ */
function user_list()
{
	global $db, $smarty;
	//搜索条件
	$keyword 	= crequest('keyword');
	$con=" WHERE is_delete = '0'";
	if($keyword){
		$con.= " and (nickname LIKE '%{$keyword}%' or mobile LIKE '%{$keyword}%')";
	}

	$smarty->assign('keyword'    ,   $keyword);

	$order 	 	= 'ORDER BY userid DESC';

	//列表信息
	$now_page 	= irequest('page');
	$now_page 	= $now_page == 0 ? 1 : $now_page;
	$page_size 	= 30;
	$start    	= ($now_page - 1) * $page_size;
	$sql 		= "SELECT * FROM `user_dingwei` {$con} {$order} LIMIT {$start},{$page_size}";
	$arr 		= $db->get_all($sql);
	foreach($arr as $key=>$val){
		$sql 		= "SELECT * FROM `user` WHERE userid={$val['sid']}";
		$user 		= $db->get_row($sql);
		$arr[$key]['sname'] = $user['username'];
	}

	$sql 		= "SELECT COUNT(userid) FROM user_dingwei {$con}";
	$total 		= $db->get_one($sql);
	$page     	= new page(array('total'=>$total, 'page_size'=>$page_size));

	$smarty->assign('user_list',$arr);
	$smarty->assign('pageshow',$page->show(6));
	$smarty->assign('now_page',$page->now_page);

	$smarty->assign('page_title', '用户列表');
	$smarty->display('dingwei/user_list.htm');
}

/*------------------------------------------------------ */
//-- 添加案例
/*------------------------------------------------------ */
//function add_user()
//{
//	global $smarty;
//
//	$smarty->assign('action', 'do_add_user');
//	$smarty->assign('page_title', '添加用户');
//	$smarty->display('dingwei/user.htm');
//}
//
///*------------------------------------------------------ */
////-- 添加案例
///*------------------------------------------------------ */
//function do_add_user()
//{
//	global $db, $smarty;
//	$data = $_POST['info'];
//	$data['password'] = md5($data['password']);
//
//	$data['add_time']	= time();
//	$data['add_time_format']	= now_time();
//
//	check_null($data['username']  	,   '用户名');
//	check_null($data['mobile']  	,   '手机号');
//	check_null($data['password']  	,   '密码');
//	$sql = "SELECT * FROM user_dingwei WHERE mobile = '{$data['mobile']}'";
//	$user = $db->get_row($sql);
//	if($user){
//		$info['is_delete'] = '0';
//		$db->update('user',$data,"userid = {$user['userid']}");
//		$id = $user['userid'];
//	}else{
//		$id = $db->insert('user',$data);
//	}
//
//	$aid  = $_SESSION['admin_id'];
//	$text = '添加用户，添加用户ID：' . $db->link_id->insert_id;
//	operate_log($aid, 'user', 1, $text);
//
//	$url_to = "user.php?action=list";
//	url_locate($url_to, '添加成功');
//}
//
/*------------------------------------------------------ */
//-- 修改案例
/*------------------------------------------------------ */
function mod_dingwei()
{
	global $db, $smarty;

	$userid = irequest('userid');
	$sql = "SELECT * FROM user_dingwei WHERE userid = '{$userid}'";
	$user = $db->get_row($sql);
	$smarty->assign('user', $user);
	$smarty->assign('url_path', URL_PATH);

	$smarty->assign('now_page', irequest('now_page'));
	$smarty->assign('page_title', '地图查看');
	$smarty->display('dingwei/ditu.htm');
}

///*------------------------------------------------------ */
////-- 修改案例
///*------------------------------------------------------ */
//function do_mod_user()
//{
//	global $db;
//
//	$userid 	  	= irequest('userid');
//	$data = $_POST['info'];
//	$data['password'] = md5($data['password']);
//
//	$data['add_time']	= time();
//	$data['add_time_format']	= now_time();
//
//	check_null($data['username']  	,   '用户名');
//	check_null($data['mobile']  	,   '手机号');
//	check_null($data['password']  	,   '密码');
//	$sql = "SELECT * FROM user WHERE mobile = '{$data['mobile']}'";
//	$user = $db->get_row($sql);
//	if($user &&  $user['userid'] != $userid){
//		alert_back('系统已存在该手机号，请勿重复添加！');
//	}else{
//		$db->update('user',$data,"userid = {$user['userid']}");
//	}
//
//	$aid  = $_SESSION['admin_id'];
//	$text = '修改用户，修改用户ID：' . $userid;
//	operate_log($aid, 'user', 2, $text);
//
//	$now_page = irequest('now_page');
//	$url_to = "user.php?action=list&page={$now_page}";
//	url_locate($url_to, '修改成功');
//}

/*------------------------------------------------------ */
//-- 删除案例
/*------------------------------------------------------ */
function del_user()
{
	global $db;

	$userid = irequest('userid');
	/*$sql = "DELETE FROM user WHERE userid = '{$userid}'";
	$db->query($sql);*/

	$update_col = "is_delete = '1'";
	$sql = "UPDATE user_dingwei SET {$update_col} WHERE userid = '{$userid}'";
	$db->query($sql);

	$aid  = $_SESSION['admin_id'];
	$text = '删除用户，删除用户ID：' . $userid;
	operate_log($aid, 'user_dingwei', 3, $text);

	$now_page = irequest('now_page');
	$url_to = "dingwei.php?action=list&page={$now_page}";
	href_locate($url_to);
}

/*------------------------------------------------------ */
//-- 批量删除案例
/*------------------------------------------------------ */
function del_sel_user()
{
	global $db;

	$userid = crequest('checkboxes');
	if (empty($userid))alert_back('请选中需要删除的选项');

	/*$sql = "DELETE FROM user WHERE userid IN ({$userid})";
	$db->query($sql);*/

	$sql = "SELECT * FROM user_dingwei WHERE userid IN ({$userid})";
	$user_all = $db->get_all($sql);
	$update_col = "is_delete = '1'";
	foreach($user_all as $key=>$val){
		$sql = "UPDATE user_dingwei SET {$update_col} WHERE userid = '{$val['userid']}'";
		$db->query($sql);
	}

	$aid  = $_SESSION['admin_id'];
	$text = '批量删除会员，批量删除会员ID：' . $userid;
	operate_log($aid, 'user_dingwei', 4, $text);

	$now_page = irequest('now_page');
	$url_to = "dingwei.php?action=list&page={$now_page}";
	href_locate($url_to);
}

?>

